package com.daka.service.impl;

import com.daka.dao.MemberDao;
import com.daka.entity.Member;
import com.daka.service.MemberService;
import com.daka.tools.DateUtil;
import com.daka.tools.MD5Util;
import com.daka.tools.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/13 17:06
 */

@Service("memberService")
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;


    @Override
    public int insert(Member po) {
        po.setMemberID(UUIDHexGenerator.generator());
        po.setEmail(po.getMobile() + "@dakaqi.cn");
        po.setNickName(po.getRealName());
        po.setBirthDay(po.getCardNO().substring(6, 10) + "-" + po.getCardNO().substring(10, 12) + "-" + po.getCardNO().substring(12, 14));
        po.setBundMobile(po.getMobile());

//        po.setNativePlace(po.getProvince()+po.getCity());
        if(po.getPassword()!=null) {
            po.setPassword(MD5Util.MD5(po.getPassword()));
        }else{
            po.setPassword(MD5Util.MD5("123456"));
        }
        po.setUserType(0);
        po.setResource(1);
        //pu
        po.setSecret("pu");
        po.setPlatform(0);
        po.setVolunteerTime(0);
        po.setAllTimes(0);
        po.setIntegral(0);
        po.setDelStatus(0);
        po.setRank(0);
        po.setEnergys(0);
        po.setUseEnergys(0);
        po.setUseIntegral(0);
        po.setSaleTimes(0);
        po.setHeadUrl("http://www.dakaqi.cn/images/default_icon_120.jpg");
        po.setHeadUrlm("http://www.dakaqi.cn/images/default_icon_75.jpg");
        po.setHeadUrls("http://www.dakaqi.cn/images/default_icon_41.jpg");
        po.setIntegral(50);
        po.setSkill("03");
        po.setEducation("2");
        po.setRegdate(DateUtil.getDate());
        po.setStars("http://www.dakaqi.cn/images/stars/star_00.png");
        po.setNation("01");
        po.setPart("3");
        po.setSex(getSexFromCardNO(po.getCardNO()));
        po.setMoney(Float.valueOf(0));

        return memberDao.insert(po);
    }

    @Override
    public int update(Member po) {
        return memberDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public Member selectByPrimaryKey(Integer id) {
        return memberDao.selectByPrimaryKey(id);
    }

    @Override
    public Member selectByMemberId(String memberId) {
        return memberDao.selectByMemberId(memberId);
    }

    @Override
    public List<Member> selectByStatement(Member po) {
        return memberDao.selectByStatement(po);
    }

    private String getSexFromCardNO(String icard) {
        int len = icard.length();
        String sex = "男";
        switch (len) {
            case 15:
                //String birth1 = icard.substring(6, 12);
                sex = icard.substring(14, 15);
                if (Integer.parseInt(sex) % 2 == 0) {
                    sex = "女";
                } else {
                    sex = "男";

                }
                break;
            case 18:
                //String birth2 = icard.substring(6, 14);
                sex = icard.substring(16, 17);
                if (Integer.parseInt(sex) % 2 == 0) {
                    sex = "女";
                } else {
                    sex = "男";

                }
                break;
        }
        return sex;
    }


}
